import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		// 提交表单列表
		formData : [
			{ label: '设备名称',		name: 'device_id',		type: 'select', required:true, loading:true, half:true, onValue:({rows, ref})=>{
				if(rows){
					const { device_number, device_model } = rows
					ref.setValue({ device_number, device_model })
				}else{
					ref.reset(['device_number','device_model'])
				}
			} },
			{ label: '设备编号',		name: 'device_number', 		half:true,	disabled:true },
			{ label: '设备型号',		name: 'device_model', 		half:true,	disabled:true },
			{ label: '科室',		name: 'project_name', 		half:true, },
			{ label: '维修申请人',	name: 'service_user', 		half:true },
			{ label: '维护日期',		name: 'service_at', 		half:true },
			{ label: '故障原因',     name: 'repair_content', 	full:true,	 type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
	}
	
	// 获取弹窗下拉框数据
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, 'device_id', ()=>{
			$cache.then( async f => {
				// 设备名称
				$fn.setRow(formData, 'device_id', await f.getDivCache())
				//
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'de-device-service/index')
	
	// 表单
	cols = [
		{ title: '设备编号',		dataIndex: 'device_number', 	width:100, align:'center' },
		{ title: '设备名称', 	dataIndex: 'device_name' },
		{ title: '设备型号', 	dataIndex: 'device_model', 		width:150 },
		{ title: '生产厂家', 	dataIndex: 'serial_man', 		width:150 },
		{ title: '科室', 		dataIndex: 'project_name', 		width:80, 	align:'center' },
		{ title: '维修申请人', 	dataIndex: 'service_user', 		width:150 },
		{ title: '维护日期', 	dataIndex: 'service_at', 		width:150 },
		{ title: '状态', 	dataIndex: 'device_status', 		width:100, 	align:'center' },
		TableControls.controls.call(this)
	]
	
	// 表格操作按钮
	table_controls = (text,{ uuid }) => [
		TableControls.edit.call(this, 'de-device-service/info', { uuid }, { label:'维修明细', select:this.getModalSelect })
	]
	
	// 操作按钮
	controls = () =>[
		Controls.add.call(this,{label:'维修申请', click:this.getModalSelect}),
		Controls.back.call(this)
	]
	
	render(){
		return (
			<>
				<Page title='设备维护' controls={this.controls()}>
					<div className='h10'></div>
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ width:800, addText:'维修明细申请', editText:'维修申请',  api: 'de-device-service/add', editApi:'de-device-service/edit' }) }
			</>
		)
	}
}

export default Index